/**
 * Created by 张乐平 on 8/2 0002.
 */

jQuery(function () {
    function getTree() {
        // Some logic to retrieve, or generate tree structure
        return [
            {
                text: "用户管理",
                href: "/system/authorization/users",
                state: {
                    checked: true,
                    disabled: false,
                    expanded: true,
                    selected: true
                }
            },
            {
                text: "角色管理",
                href: "/system/authorization/roles/query"
            },
            {
                text: "授权管理",
                href: "/system/authorization/perms/query"
            }
        ];
    }

    $('#tree').treeview({enableLinks:true,data: getTree()});

    $('#myModal').modal('hide');

    $('#myModal').on('hide.bs.modal', function () {
        //alert('嘿，我听说您喜欢模态框...');
    })
});

var confirmDelUser = function (userName, userId) {
    $("#delUserName").text(userName);
    $("#myModal-del").modal("show");
    $("#doDel").one("click", function () {
        window.location = "./users/del/" + userId;
    });
};

var showUser = function (userId) {
    $.ajax({
        type: "get",
        url: "./users/" + userId,
        dataType: "json",
        async: true,
        success: function (data) {
            $("#addUserName-update").val(data.addUserName);
            $("#userId").val(data.userId);
            $("#pwd-update").val(data.password);
            $("#pwd2-update").val(data.password2);
            var chkRoles = $("#roles").find("input");
            $(chkRoles).each(function (i, e) {
                var echk = e;
                $(echk).prop("checked", false);
                $(data.roles).each(function (i, e) {
                    if ($(echk).val() == e) {
                        $(echk).prop("checked", true);
                    }
                });
            });
            $("#myModal-update").modal("show");
        }
    });
};
